﻿Public Class SampleExplorer
    Inherits ExplorerTreeList

    Private SampleHeaderPL As PLBase

    Public Sub New(ByVal aQueryName As String)
        MyBase.New(aQueryName)
        'AddHandler InitTreeActionOptionList, AddressOf Init_TreeActionOptionList
    End Sub

    Public Sub New()
        MyBase.New()
        'AddHandler InitTreeActionOptionList, AddressOf Init_TreeActionOptionList
    End Sub

    Public Overrides Sub PopulateDetailSection()
        Me.Cursor = Cursors.WaitCursor
        '
        If aGridControl IsNot Nothing Then
            aGridControl.Dispose()
            aGridControl = Nothing
        End If
        '
        Dim guidEnfant As Guid = DirectCast(NodeDataRow(Me.KeyFieldName), Guid)
        '
        Select Case FocusedNodeLevel
            Case 0
                ShowAllSampleHeader()
            Case 1
                ShowBySampleHeader(guidEnfant)
                aGridControl.ExpandFirstMasterRow = True
            Case Else
        End Select

        'On trouve la form pour effacer les controle dedans et ensuite remettre le bon.
        'Comme ca on change le Detail selon la ligne dans L'explorer
        For Each aFrm As XtraForm In Me.ParentForm.MdiChildren
            If aFrm.Name = PanelName Then
                aFrm.Controls.Clear()
                aFrm.Controls.Add(aGridControl)
                aFrm.Show()
                aFrm.Focus()
            End If
            aFrm.Refresh()
        Next
        '
        Me.Cursor = Cursors.Default
    End Sub

    ''' <summary>
    ''' This sub shows the while list of Sample 
    ''' </summary>
    Public Overridable Sub ShowAllSampleHeader()
        SampleHeaderPL = PLBase.CreateInstanceOf(PLTypeOf.SampleHeader_PL, "SampleHeaderList")
        aGridControl = New GridExtPL() With {.Dock = DockStyle.Fill, .Pl = SampleHeaderPL, .SecureGrid = True, .Name = "Sample List", .FormCtrlCD = Nothing}
        aGridControl.AllowDelete = False
        aGridControl.AllowCreate = False
        aGridControl.AllowUpdate = False
    End Sub

    ''' <summary>
    ''' This sub shows Sample by Sample
    ''' </summary>
    Public Overridable Sub ShowBySampleHeader(ByVal sampleID As System.Guid)
        SampleHeaderPL = PLBase.CreateInstanceOf(PLTypeOf.SampleHeader_PL, "SampleHeader")
        aGridControl = New GridExtPL() With {.Dock = DockStyle.Fill, .Pl = SampleHeaderPL, .SecureGrid = True, .Name = "Sample List", .FormCtrlCD = MyBase.PanelName}
        SampleHeaderPL.SQLWhere = String.Format("QAQCInspLotID = '{0}'", sampleID)


        AddHandler aGridControl.ShownMasterEditor, AddressOf SampleHeader_ShownMasterEditor
        AddHandler aGridControl.MasterCustomRowCellEditForEditing, AddressOf SampleHeader_MasterCustomRowCellEditForEditing
        AddHandler aGridControl.MainContextMenuBeforePopup, AddressOf SampleHeader_ContextMenuBeforePopup
        AddHandler aGridControl.ValidateMasterRow, AddressOf SampleHeader_ValidateMasterRow
        AddHandler aGridControl.InitNewMasterRow, AddressOf SampleHeader_InitNewMasterRow
        AddHandler aGridControl.MasterCellValueChanged, AddressOf SampleHeader_MasterCellValueChanged

        AddHandler aGridControl.ShownDetailEditor, AddressOf SampleDetail_ShownDetailEditor
        AddHandler aGridControl.ProcessCurrentDetailRecord, AddressOf SampleDetail_ProcessCurrentDetailRecord
        AddHandler aGridControl.DetailRowCellStyle, AddressOf SampleDetail_DetailRowCellStyle
        AddHandler aGridControl.DetailRowStyle, AddressOf SampleDetail_DetailRowStyle
        AddHandler aGridControl.DeleteRow, AddressOf SampleHeader_Delete
        AddHandler aGridControl.ValidateDetailRow, AddressOf SampleDetail_ValidateRow
        AddHandler aGridControl.DetailRowClick, AddressOf SampleDetail_DetailRowClick
        'AddHandler aGridControl.DetailCustomRowCellEditForEditing, AddressOf SampleDetail_DetailCustomRowCellEditForEditing


        aGridControl.GridActionOptionList.Add(New ActionOption("Sample", "ACT_00006", "Remove", AddressOf SampleHeader_Remove, True, True))
        aGridControl.GridActionOptionList.Add(New ActionOption("Print", "ACT_00007", "Print Results", AddressOf PrintResult, True, True))
    End Sub

    Private Sub SampleExplorer_Load(sender As Object, e As EventArgs) Handles Me.Load
        TreeActionOptionListLoad()
        MetaControlTreeListActionOptionList.ApplySecurity()
    End Sub
End Class
